package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.http.User;
import com.diudiutechnology.www.yadiudiu.util.http.UserCallback;
import com.diudiutechnology.www.yadiudiu.util.utils.SharedPreferencesUtil;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.SelectTheAddressActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.CleanBusiness;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/8/30.
 * 呼叫开锁
 */

public class CallLockActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.addres_layout)
    LinearLayout addres_layout;
    @Bind(R.id.title_center)
    TextView title_center;
    @Bind(R.id.send_call)
    Button button;
    @Bind(R.id.address)
    EditText mHousenumberTxt;
    @Bind(R.id.contact)
    EditText mContactsTxt;
    @Bind(R.id.phone)
    EditText mPhoneTxt;
    private String mAddress = "wa";
    private String mLongitude = "10.22222";
    private String mLatitude = "12.32232";
    private String mHousenumber;
    private String mContacts;
    private String mPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_lock);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        addres_layout.setOnClickListener(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (CheckData()) {
                    CallOrder();
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.addres_layout:
                startActivity(new Intent(this, SelectTheAddressActivity.class));
                break;
        }
    }

    private boolean CheckData() {
        mHousenumber = mHousenumberTxt.getText().toString().trim();
        mContacts = mContactsTxt.getText().toString().trim();
        mPhone = mPhoneTxt.getText().toString().trim();
        if (TextUtils.isEmpty(mLongitude) || TextUtils.isEmpty(mLatitude) || TextUtils.isEmpty(mAddress)) {
            ToastUtils.show(this,"请选择取件地址", Toast.LENGTH_SHORT);
            return false;
        }
        if (TextUtils.isEmpty(mHousenumber)) {
            ToastUtils.show(this,"请输入详细地址", Toast.LENGTH_SHORT);
            return false;
        }
        if (TextUtils.isEmpty(mContacts)) {
            ToastUtils.show(this,"请输入联系人", Toast.LENGTH_SHORT);
            return false;
        }
        if (TextUtils.isEmpty(mPhone)) {
            ToastUtils.show(this,"请输入联系电话", Toast.LENGTH_SHORT);
            return false;
        }
        return true;
    }

    private void CallOrder() {
        CleanBusiness.postLock(this, SharedPreferencesUtil.Keys.USERID_KEYS, mLongitude, mLatitude, mAddress, mHousenumber, mContacts, mPhone, new UserCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(CallLockActivity.this, e.getMessage(), Toast.LENGTH_SHORT);
            }

            @Override
            public void onResponse(User response, int id) {
                ToastUtils.show(CallLockActivity.this,"呼叫成功", Toast.LENGTH_SHORT);
                CallLockActivity.this.finish();
            }
        });
    }
}

